// 参考文档： https://www.quanzhan.co/luch-request/guide/3.x/#%E4%BB%8B%E7%BB%8D
import Service from 'luch-request'
import store from '../store/index.js'
const request = new Service({
  baseURL: getApp().globalData.IP
})
// 请求拦截
request.interceptors.request.use((config) => { // 可使用async await 做异步操作
  config.header = {
    "X-Token": uni.getStorageSync('token') || '617e6c49a32e235a32a3bc15',
	"X-clientType": "Mini"
  }
  return config
}, config => { // 可使用async await 做异步操作
  return Promise.reject(config)
})
request.interceptors.response.use((response) => {
  const res = response.data
  if (res.code === 200) {
    return res
  } else if (res.code === 401) {
    uni.showToast({
      title: '身份过期，请先登录',
      icon: 'none'
    })
	setTimeout(()=>{
		console.log(1111)
		store.commit('SET_ISLOGIN',false)
		uni.navigateBack()
	},2000)
	return Promise.reject(res);
  } else if (res.code === 420) {
    uni.showToast({
		title: res.msg + "，请先授权",
		icon: 'none',
		duration: 4000
    })
	return Promise.reject(res)
  }else if(res.code === 0){
	  uni.showToast({
	  	title: res.msg,
	  	icon: 'none',
	  	duration: 4000
	  })
	  return Promise.reject(res)
  }
}, (response) => { /*  对响应错误做点什么 （statusCode !== 200）*/
  uni.showToast({
	  title: '网络断开',
	  icon: 'none',
	  duration: 4000
  })
  return Promise.reject(response)
})

export default request
